﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Kindohm.KSynth.UI
{
    public partial class VoiceSettings : UserControl
    {
        public VoiceSettings()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VoiceSettings_Loaded);
            this.waveformList.SelectionChanged += new SelectionChangedEventHandler(waveformList_SelectionChanged);
        }

        void waveformList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.WaveformChanged != null)
                this.WaveformChanged(this, EventArgs.Empty);
        }

        public WaveFormInfo WaveFormInfo
        {
            get
            {
                return this.waveformList.SelectedItem as WaveFormInfo;
            }
            set
            {
                this.waveformList.SelectedItem = value;
            }
        }

        public double Detune
        {
            get { return this.detuneSlider.Value; }
            set
            {
                this.detuneSlider.Value = value;
                this.detuneSlider_MouseLeftButtonUp(null, null);
            }
        }

        public double Pan
        {
            get { return this.panSlider.Value; }
            set
            {
                this.panSlider.Value = value;
                this.panSlider_MouseLeftButtonUp(null, null);
            }
        }

        public int Level
        {
            get { return (int)this.levelSlider.Value; }
        }

        public ushort Phase
        {
            get { return (ushort)this.phaseSlider.Value; }
            set
            {
                this.phaseSlider.Value = value;
                this.phaseSlider_MouseLeftButtonUp(null, null);
            }
        }

        public string Title
        {
            get { return this.title.Text; }
            set { this.title.Text = value; }
        }

        void VoiceSettings_Loaded(object sender, RoutedEventArgs e)
        {
            this.phaseSlider.Minimum = 0;
            this.phaseSlider.Maximum = ushort.MaxValue;
            this.phaseSlider.Value = 0;

            IList<WaveFormInfo> items = WaveFormRepository.Instance.WaveForms;
            this.waveformList.ItemsSource = items;
            this.waveformList.SelectedItem = items[0];
        }

        private void Button_Loaded(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            WaveFormInfo info = (WaveFormInfo)button.DataContext;
            if (!info.UserControlled)
                button.Visibility = Visibility.Collapsed;
        }

        public event EventHandler WaveformChanged;
        public event EventHandler<PanChangedEventArgs> PanChanged;
        public event EventHandler LevelChanged;
        public event EventHandler PhaseChanged;
        public event EventHandler DetuneChanged;

        private void panSlider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.PanChanged != null)
            {
                PanChangedEventArgs args = new PanChangedEventArgs();
                args.Percent = panSlider.Value / 100;
                this.PanChanged(this, args);
            }
        }

        private void levelSlider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.LevelChanged != null)
                this.LevelChanged(this, EventArgs.Empty);
        }

        private void phaseSlider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.PanChanged != null)
            {
                this.PhaseChanged(this, EventArgs.Empty);
            }
        }

        private void detuneSlider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.DetuneChanged != null)
            {
                this.DetuneChanged(this, EventArgs.Empty);
            }
        }

    }
}
